<?php

App::uses('AppController', 'Controller');

/**
 * Careers Controller
 *
 */
class CareersController extends AppController {

    /**
     * Scaffold
     *
     * @var mixed
     */
    public $scaffold;
    public $uses = array('Career');

    public function index() {
        $data = $this->Career->Find('all',array(
            'conditions'=>array('Career.status'=>'Y')
        ));
        $this->set('data',$data);
    }

    public function add() {
        if ($this->request->is('post')) {
            //debug($this->request->data);
            $this->Career->set(array('created_by'=>$this->Session->read('User.username')));
            $this->Career->set($this->request->data);
            if ($this->Career->validates()) {
                $this->Career->save();
                $this->Session->setFlash('Save success.');
                $this->redirect('/Careers/listAll');
            }
        }
    }

    public function edit($id = NULL) {
        if ($id != NULL) {
            if ($this->request->is('put')) {
                $this->Career->set($this->request->data);
                if ($this->Career->validates()) {
                    $this->Career->save();
                    $this->Session->setFlash('Update success.');
                    $this->redirect('/Careers/listAll');
                }
            } else {
                $this->Career->id = $id;
                $this->data = $this->Career->read();
            }
        } else {
            
        }
    }

    public function change($id = NULL, $status = NULL) {
        if ($id != NULL && $status != NULL) {
            $s = $status == 'N' ? 'Y' : 'N';
            $data = array('id' => $id, 'status' => $s);
            $this->Career->save($data);
            $this->Session->setFlash('Update success.');
        }
        $this->redirect('/Careers/listAll');
    }

    public function delete($id = NULL) {
        if ($id != NULL) {
            if ($this->Career->delete($id)) {
                $this->Session->setFlash('Remove Success!!');
            }
        }
        $this->redirect('/Careers/listAll');
    }

    public function view($id = NULL) {
        if ($id != NULL) {
            $data = $this->Career->findById($id);
            $this->set('data', $data);
        } else {
            $this->redirect('/Careers/listAll');
        }
    }

    public function listAll() {
        $data = $this->Career->Find('all',array(
            'order'=>array('Career.created','Career.status')
        ));

        $this->set('all', $data);
    }

}
